home *** CD-ROM | disk | FTP | other *** search
/ Languguage OS 2 / Languguage OS II Version 10-94 (Knowledge Media)(1994).ISO / gnu / dejagnu.lha / dejagnu-1.0.1 / tcl / tests / set.test < prev    next >
Text File  |  1992-11-06  |  17KB  |  531 lines

  1. # Commands covered:  set, unset, array
  2. #
  3. # This file contains a collection of tests for one or more of the Tcl
  4. # built-in commands.  Sourcing this file into Tcl runs the tests and
  5. # generates output for errors.  No output means no errors were found.
  6. #
  7. # Copyright 1991 Regents of the University of California
  8. # Permission to use, copy, modify, and distribute this
  9. # software and its documentation for any purpose and without
  10. # fee is hereby granted, provided that this copyright notice
  11. # appears in all copies.  The University of California makes no
  12. # representations about the suitability of this software for any
  13. # purpose.  It is provided "as is" without express or implied
  14. # warranty.
  15. #
  16. # $Header: /user6/ouster/tcl/tests/RCS/set.test,v 1.8 91/10/31 16:40:57 ouster Exp Locker: ouster $ (Berkeley)
  17.  
  18. if {[string compare test [info procs test]] == 1} then {source defs}
  19.  
  20. proc ignore args {}
  21.  
  22. # Simple variable operations.
  23.  
  24. catch {unset a}
  25. test set-1.1 {basic variable setting and unsetting} {
  26.     set a 22
  27. } 22
  28. test set-1.2 {basic variable setting and unsetting} {
  29.     set a 123
  30.     set a
  31. } 123
  32. test set-1.3 {basic variable setting and unsetting} {
  33.     set a xxx
  34.     format %s $a
  35. } xxx
  36. test set-1.4 {basic variable setting and unsetting} {
  37.     set a 44
  38.     unset a
  39.     list [catch {set a} msg] $msg
  40. } {1 {can't read "a": no such variable}}
  41.  
  42. # Basic array operations.
  43.  
  44. catch {unset a}
  45. set a(xyz) 2
  46. set a(44) 3
  47. set {a(a long name)} test
  48. test set-2.1 {basic array operations} {
  49.     lsort [array names a]
  50. } {44 {a long name} xyz}
  51. test set-2.2 {basic array operations} {
  52.     set a(44)
  53. } 3
  54. test set-2.3 {basic array operations} {
  55.     set a(xyz)
  56. } 2
  57. test set-2.4 {basic array operations} {
  58.     set "a(a long name)"
  59. } test
  60. test set-2.5 {basic array operations} {
  61.     list [catch {set a(other)} msg] $msg
  62. } {1 {can't read "a(other)": no such element in array}}
  63. test set-2.6 {basic array operations} {
  64.     list [catch {set a} msg] $msg
  65. } {1 {can't read "a": no such variable}}
  66. test set-2.7 {basic array operations} {
  67.     format %s $a(44)
  68. } 3
  69. test set-2.8 {basic array operations} {
  70.     format %s $a(a long name)
  71. } test
  72. unset a(44)
  73. test set-2.9 {basic array operations} {
  74.     lsort [array names a]
  75. } {{a long name} xyz}
  76. unset a
  77. test set-2.10 {basic array operations} {
  78.     list [catch {set a(xyz)} msg] $msg
  79. } {1 {can't read "a(xyz)": no such variable}}
  80.  
  81. # Test the set commands, and exercise the corner cases of the code
  82. # that parses array references into two parts.
  83.  
  84. test set-3.1 {set command} {
  85.     list [catch {set} msg] $msg
  86. } {1 {wrong # args: should be "set varName ?newValue?"}}
  87. test set-3.2 {set command} {
  88.     list [catch {set x y z} msg] $msg
  89. } {1 {wrong # args: should be "set varName ?newValue?"}}
  90. test set-3.3 {set command} {
  91.     catch {unset a}
  92.     list [catch {set a} msg] $msg
  93. } {1 {can't read "a": no such variable}}
  94. test set-3.4 {set command} {
  95.     catch {unset a}
  96.     set a(14) 83
  97.     list [catch {set a 22} msg] $msg
  98. } {1 {can't set "a": variable is array}}
  99.  
  100. # Test the corner-cases of parsing array names, using set and unset.
  101.  
  102. test set-4.1 {parsing array names} {
  103.     catch {unset a}
  104.     set a(()) 44
  105.     list [catch {array names a} msg] $msg
  106. } {0 ()}
  107. test set-4.2 {parsing array names} {
  108.     catch {unset a a(abcd}
  109.     set a(abcd 33
  110.     info exists a(abcd
  111. } 1
  112. test set-4.3 {parsing array names} {
  113.     catch {unset a a(abcd}
  114.     set a(abcd 33
  115.     list [catch {array names a} msg] $msg
  116. } {1 {"a" isn't an array}}
  117. test set-4.4 {parsing array names} {
  118.     catch {unset a abcd)}
  119.     set abcd) 33
  120.     info exists abcd)
  121. } 1
  122. test set-4.5 {parsing array names} {
  123.     set a(bcd yyy
  124.     catch {unset a}
  125.     list [catch {set a(bcd} msg] $msg
  126. } {0 yyy}
  127. test set-4.6 {parsing array names} {
  128.     catch {unset a}
  129.     set a 44
  130.     list [catch {set a(bcd test} msg] $msg
  131. } {0 test}
  132.  
  133. # Errors in reading variables
  134.  
  135. test set-5.1 {errors in reading variables} {
  136.     catch {unset a}
  137.     list [catch {set a} msg] $msg
  138. } {1 {can't read "a": no such variable}}
  139. test set-5.2 {errors in reading variables} {
  140.     catch {unset a}
  141.     set a 44
  142.     list [catch {set a(18)} msg] $msg
  143. } {1 {can't read "a(18)": variable isn't array}}
  144. test set-5.3 {errors in reading variables} {
  145.     catch {unset a}
  146.     set a(6) 44
  147.     list [catch {set a(18)} msg] $msg
  148. } {1 {can't read "a(18)": no such element in array}}
  149. test set-5.4 {errors in reading variables} {
  150.     catch {unset a}
  151.     set a(6) 44
  152.     list [catch {set a} msg] $msg
  153. } {1 {can't read "a": no such variable}}
  154.  
  155. # Errors and other special cases in writing variables
  156.  
  157. test set-6.1 {creating array during write} {
  158.     catch {unset a}
  159.     trace var a rwu ignore
  160.     list [catch {set a(14) 186} msg] $msg [array names a]
  161. } {0 186 14}
  162. test set-6.2 {errors in writing variables} {
  163.     catch {unset a}
  164.     set a xxx
  165.     list [catch {set a(14) 186} msg] $msg
  166. } {1 {can't set "a(14)": variable isn't array}}
  167. test set-6.3 {errors in writing variables} {
  168.     catch {unset a}
  169.     set a(100) yyy
  170.     list [catch {set a 2} msg] $msg
  171. } {1 {can't set "a": variable is array}}
  172. test set-6.4 {expanding variable size} {
  173.     catch {unset a}
  174.     list [set a short] [set a "longer name"] [set a "even longer name"] \
  175.         [set a "a much much truly longer name"]
  176. } {short {longer name} {even longer name} {a much much truly longer name}}
  177.  
  178. # Unset command, Tcl_UnsetVar procedures
  179.  
  180. test set-7.1 {unset command} {
  181.     catch {unset a}; catch {unset b}; catch {unset c}; catch {unset d}
  182.     set a 44
  183.     set b 55
  184.     set c 66
  185.     set d 77
  186.     unset a b c
  187.     list [catch {set a(0) 0}] [catch {set b(0) 0}] [catch {set c(0) 0}] \
  188.         [catch {set d(0) 0}]
  189. } {0 0 0 1}
  190. test set-7.2 {unset command} {
  191.     list [catch {unset} msg] $msg
  192. } {1 {wrong # args: should be "unset varName ?varName ...?"}}
  193. test set-7.3 {unset command} {
  194.     catch {unset a}
  195.     list [catch {unset a} msg] $msg
  196. } {1 {can't unset "a": no such variable}}
  197. test set-7.4 {unset command} {
  198.     catch {unset a}
  199.     set a 44
  200.     list [catch {unset a(14)} msg] $msg
  201. } {1 {can't unset "a(14)": variable isn't array}}
  202. test set-7.5 {unset command} {
  203.     catch {unset a}
  204.     set a(0) xx
  205.     list [catch {unset a(14)} msg] $msg
  206. } {1 {can't unset "a(14)": no such element in array}}
  207. test set-7.6 {unset command} {
  208.     catch {unset a}; catch {unset b}; catch {unset c}
  209.     set a foo
  210.     set c gorp
  211.     list [catch {unset a a a(14)} msg] $msg [info exists c]
  212. } {1 {can't unset "a": no such variable} 1}
  213. test set-7.7 {unsetting globals from within procedures} {
  214.     set y 0
  215.     proc p1 {} {
  216.     global y
  217.     set z [p2]
  218.     return [list $z [catch {set y} msg] $msg]
  219.     }
  220.     proc p2 {} {global y; unset y; list [catch {set y} msg] $msg}
  221.     p1
  222. } {{1 {can't read "y": no such variable}} 1 {can't read "y": no such variable}}
  223. test set-7.8 {unsetting globals from within procedures} {
  224.     set y 0
  225.     proc p1 {} {
  226.     global y
  227.     p2
  228.     return [list [catch {set y 44} msg] $msg]
  229.     }
  230.     proc p2 {} {global y; unset y}
  231.     concat [p1] [list [catch {set y} msg] $msg]
  232. } {0 44 0 44}
  233. test set-7.9 {unsetting globals from within procedures} {
  234.     set y 0
  235.     proc p1 {} {
  236.     global y
  237.     unset y
  238.     return [list [catch {set y 55} msg] $msg]
  239.     }
  240.     concat [p1] [list [catch {set y} msg] $msg]
  241. } {0 55 0 55}
  242. test set-7.10 {unset command} {
  243.     catch {unset a}
  244.     set a(14) 22
  245.     unset a(14)
  246.     list [catch {set a(14)} msg] $msg [catch {array names a} msg2] $msg2
  247. } {1 {can't read "a(14)": no such element in array} 0 {}}
  248. test set-7.11 {unset command} {
  249.     catch {unset a}
  250.     set a(14) 22
  251.     unset a
  252.     list [catch {set a(14)} msg] $msg [catch {array names a} msg2] $msg2
  253. } {1 {can't read "a(14)": no such variable} 1 {"a" isn't an array}}
  254.  
  255. # Array command.
  256.  
  257. test set-8.1 {array command} {
  258.     list [catch {array} msg] $msg
  259. } {1 {wrong # args: should be "array option arrayName ?arg ...?"}}
  260. test set-8.2 {array command} {
  261.     catch {unset a}
  262.     list [catch {array names a} msg] $msg
  263. } {1 {"a" isn't an array}}
  264. test set-8.3 {array command} {
  265.     catch {unset a}
  266.     set a 44
  267.     list [catch {array names a} msg] $msg
  268. } {1 {"a" isn't an array}}
  269. test set-8.4 {array command} {
  270.     catch {unset a}
  271.     set a(22) 3
  272.     list [catch {array gorp a} msg] $msg
  273. } {1 {bad option "gorp": should be anymore, donesearch, names, nextelement, size, or startsearch}}
  274. test set-8.5 {array command, names option} {
  275.     catch {unset a}
  276.     set a(22) 3
  277.     list [catch {array names a 4} msg] $msg
  278. } {1 {wrong # args: should be "array names arrayName"}}
  279. test set-8.6 {array command, names option} {
  280.     catch {unset a}
  281.     set a(22) 3; set a(Textual_name) 44; set "a(name with spaces)" xxx
  282.     list [catch {lsort [array names a]} msg] $msg
  283. } {0 {22 Textual_name {name with spaces}}}
  284. test set-8.7 {array command, names option} {
  285.     catch {unset a}
  286.     set a(22) 3; set a(33) 44;
  287.     trace var a(xxx) w ignore
  288.     list [catch {lsort [array names a]} msg] $msg
  289. } {0 {22 33}}
  290. test set-8.8 {array command, names option} {
  291.     catch {unset a}
  292.     set a(22) 3; set a(33) 44;
  293.     trace var a(xxx) w ignore
  294.     set a(xxx) value
  295.     list [catch {lsort [array names a]} msg] $msg
  296. } {0 {22 33 xxx}}
  297. test set-8.9 {array command, size option} {
  298.     catch {unset a}
  299.     set a(22) 3
  300.     list [catch {array size a 4} msg] $msg
  301. } {1 {wrong # args: should be "array size arrayName"}}
  302. test set-8.10 {array command, size option} {
  303.     catch {unset a}
  304.     set a(22) 3; set a(Textual_name) 44; set "a(name with spaces)" xxx
  305.     list [catch {array size a} msg] $msg
  306. } {0 3}
  307. test set-8.10 {array command, size option} {
  308.     catch {unset a}
  309.     set a(22) 3; set a(xx) 44; set a(y) xxx
  310.     unset a(22) a(y) a(xx)
  311.     list [catch {array size a} msg] $msg
  312. } {0 0}
  313. test set-8.11 {array command, size option} {
  314.     catch {unset a}
  315.     set a(22) 3;
  316.     trace var a(33) rwu ignore
  317.     list [catch {array size a} msg] $msg
  318. } {0 1}
  319.  
  320. test set-9.1 {ids for array enumeration} {
  321.     catch {unset a}
  322.     set a(a) 1
  323.     list [array st a] [array st a] [array done a s-1-a; array st a] \
  324.         [array done a s-2-a; array d a s-3-a; array start a]
  325. } {s-1-a s-2-a s-3-a s-1-a}
  326. test set-9.2 {array enumeration} {
  327.     catch {unset a}
  328.     set a(a) 1
  329.     set a(b) 1
  330.     set a(c) 1
  331.     set x [array startsearch a]
  332.     list [array nextelement a $x] [array ne a $x] [array next a $x] \
  333.         [array next a $x] [array next a $x]
  334. } {a b c {} {}}
  335. test set-9.3 {array enumeration} {
  336.     catch {unset a}
  337.     set a(a) 1
  338.     set a(b) 1
  339.     set a(c) 1
  340.     set x [array startsearch a]
  341.     set y [array startsearch a]
  342.     set z [array startsearch a]
  343.     list [array nextelement a $x] [array ne a $x] \
  344.         [array next a $y] [array next a $z] [array next a $y] \
  345.         [array next a $z] [array next a $y] [array next a $z] \
  346.         [array next a $y] [array next a $z] [array next a $x] \
  347.         [array next a $x]
  348. } {a b a a b b c c {} {} c {}}
  349. test set-9.4 {array enumeration: stopping searches} {
  350.     catch {unset a}
  351.     set a(a) 1
  352.     set a(b) 1
  353.     set a(c) 1
  354.     set x [array startsearch a]
  355.     set y [array startsearch a]
  356.     set z [array startsearch a]
  357.     list [array next a $x] [array next a $x] [array next a $y] \
  358.         [array done a $z; array next a $x] \
  359.         [array done a $x; array next a $y] [array next a $y]
  360. } {a b a c b c}
  361. test set-9.5 {array enumeration: stopping searches} {
  362.     catch {unset a}
  363.     set a(a) 1
  364.     set x [array startsearch a]
  365.     array done a $x
  366.     list [catch {array next a $x} msg] $msg
  367. } {1 {couldn't find search "s-1-a"}}
  368. test set-9.6 {array enumeration: searches automatically stopped} {
  369.     catch {unset a}
  370.     set a(a) 1
  371.     set x [array startsearch a]
  372.     set y [array startsearch a]
  373.     set a(b) 1
  374.     list [catch {array next a $x} msg] $msg \
  375.         [catch {array next a $y} msg2] $msg2
  376. } {1 {couldn't find search "s-1-a"} 1 {couldn't find search "s-2-a"}}
  377. test set-9.7 {array enumeration: searches automatically stopped} {
  378.     catch {unset a}
  379.     set a(a) 1
  380.     set x [array startsearch a]
  381.     set y [array startsearch a]
  382.     set a(a) 2
  383.     list [catch {array next a $x} msg] $msg \
  384.         [catch {array next a $y} msg2] $msg2
  385. } {0 a 0 a}
  386. test set-9.8 {array enumeration: searches automatically stopped} {
  387.     catch {unset a}
  388.     set a(a) 1
  389.     set x [array startsearch a]
  390.     set y [array startsearch a]
  391.     catch {unset a(c)}
  392.     list [catch {array next a $x} msg] $msg \
  393.         [catch {array next a $y} msg2] $msg2
  394. } {1 {couldn't find search "s-1-a"} 1 {couldn't find search "s-2-a"}}
  395. test set-9.9 {array enumeration: searches automatically stopped} {
  396.     catch {unset a}
  397.     set a(a) 1
  398.     set x [array startsearch a]
  399.     set y [array startsearch a]
  400.     trace var a(b) r {}
  401.     list [catch {array next a $x} msg] $msg \
  402.         [catch {array next a $y} msg2] $msg2
  403. } {1 {couldn't find search "s-1-a"} 1 {couldn't find search "s-2-a"}}
  404. test set-9.10 {array enumeration: searches automatically stopped} {
  405.     catch {unset a}
  406.     set a(a) 1
  407.     set x [array startsearch a]
  408.     set y [array startsearch a]
  409.     trace var a(a) r {}
  410.     list [catch {array next a $x} msg] $msg \
  411.         [catch {array next a $y} msg2] $msg2
  412. } {0 a 0 a}
  413. test set-9.11 {array enumeration with traced undefined elements} {
  414.     catch {unset a}
  415.     set a(a) 1
  416.     trace var a(b) r {}
  417.     set x [array startsearch a]
  418.     list [array next a $x] [array next a $x]
  419. } {a {}}
  420.  
  421. test set-10.1 {array enumeration errors} {
  422.     list [catch {array start} msg] $msg
  423. } {1 {wrong # args: should be "array option arrayName ?arg ...?"}}
  424. test set-10.2 {array enumeration errors} {
  425.     list [catch {array start a b} msg] $msg
  426. } {1 {wrong # args: should be "array startsearch arrayName"}}
  427. test set-10.3 {array enumeration errors} {
  428.     catch {unset a}
  429.     list [catch {array start a} msg] $msg
  430. } {1 {"a" isn't an array}}
  431. test set-10.4 {array enumeration errors} {
  432.     catch {unset a}
  433.     set a(a) 1
  434.     set x [array startsearch a]
  435.     list [catch {array next a} msg] $msg
  436. } {1 {wrong # args: should be "array nextelement arrayName searchId"}}
  437. test set-10.5 {array enumeration errors} {
  438.     catch {unset a}
  439.     set a(a) 1
  440.     set x [array startsearch a]
  441.     list [catch {array next a b c} msg] $msg
  442. } {1 {wrong # args: should be "array nextelement arrayName searchId"}}
  443. test set-10.6 {array enumeration errors} {
  444.     catch {unset a}
  445.     set a(a) 1
  446.     set x [array startsearch a]
  447.     list [catch {array next a a-1-a} msg] $msg
  448. } {1 {illegal search identifier "a-1-a"}}
  449. test set-10.7 {array enumeration errors} {
  450.     catch {unset a}
  451.     set a(a) 1
  452.     set x [array startsearch a]
  453.     list [catch {array next a sx1-a} msg] $msg
  454. } {1 {illegal search identifier "sx1-a"}}
  455. test set-10.8 {array enumeration errors} {
  456.     catch {unset a}
  457.     set a(a) 1
  458.     set x [array startsearch a]
  459.     list [catch {array next a s--a} msg] $msg
  460. } {1 {illegal search identifier "s--a"}}
  461. test set-10.9 {array enumeration errors} {
  462.     catch {unset a}
  463.     set a(a) 1
  464.     set x [array startsearch a]
  465.     list [catch {array next a s-1-b} msg] $msg
  466. } {1 {search identifier "s-1-b" isn't for variable "a"}}
  467. test set-10.10 {array enumeration errors} {
  468.     catch {unset a}
  469.     set a(a) 1
  470.     set x [array startsearch a]
  471.     list [catch {array next a s-1ba} msg] $msg
  472. } {1 {illegal search identifier "s-1ba"}}
  473. test set-10.11 {array enumeration errors} {
  474.     catch {unset a}
  475.     set a(a) 1
  476.     set x [array startsearch a]
  477.     list [catch {array next a s-2-a} msg] $msg
  478. } {1 {couldn't find search "s-2-a"}}
  479. test set-10.12 {array enumeration errors} {
  480.     list [catch {array done a} msg] $msg
  481. } {1 {wrong # args: should be "array donesearch arrayName searchId"}}
  482. test set-10.13 {array enumeration errors} {
  483.     list [catch {array done a b c} msg] $msg
  484. } {1 {wrong # args: should be "array donesearch arrayName searchId"}}
  485. test set-10.14 {array enumeration errors} {
  486.     list [catch {array done a b} msg] $msg
  487. } {1 {illegal search identifier "b"}}
  488. test set-10.15 {array enumeration errors} {
  489.     list [catch {array anymore a} msg] $msg
  490. } {1 {wrong # args: should be "array anymore arrayName searchId"}}
  491. test set-10.16 {array enumeration errors} {
  492.     list [catch {array any a b c} msg] $msg
  493. } {1 {wrong # args: should be "array anymore arrayName searchId"}}
  494. test set-10.17 {array enumeration errors} {
  495.     catch {unset a}
  496.     set a(0) 44
  497.     list [catch {array any a bogus} msg] $msg
  498. } {1 {illegal search identifier "bogus"}}
  499.  
  500. # Array enumeration with "anymore" option
  501.  
  502. test set-11.1 {array anymore option} {
  503.     catch {unset a}
  504.     set a(a) 1
  505.     set a(b) 2
  506.     set a(c) 3
  507.     array startsearch a
  508.     list [array anymore a s-1-a] [array next a s-1-a] \
  509.         [array anymore a s-1-a] [array next a s-1-a] \
  510.         [array anymore a s-1-a] [array next a s-1-a] \
  511.         [array anymore a s-1-a] [array next a s-1-a] 
  512. } {1 a 1 b 1 c 0 {}}
  513. test set-11.2 {array anymore option} {
  514.     catch {unset a}
  515.     set a(a) 1
  516.     set a(b) 2
  517.     set a(c) 3
  518.     array startsearch a
  519.     list [array next a s-1-a] [array next a s-1-a] \
  520.         [array anymore a s-1-a] [array next a s-1-a] \
  521.         [array next a s-1-a] [array anymore a s-1-a] 
  522. } {a b 1 c {} 0}
  523.  
  524. # Must delete variables when done, since these arrays get used as
  525. # scalars by other tests.
  526.  
  527. catch {unset a}
  528. catch {unset b}
  529. catch {unset c}
  530. return ""
  531.